﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using CVsharp.Jquery.DWZ;
using CVsharp.Models.Entities;

namespace CVsharp.DWZMvc.Controllers
{ 
    public class ChangeLogController : Controller
    {
        private readonly DBEntities db = new DBEntities();

        //
        // GET: /ChangeLog/

        public ViewResult Index()
        {
            var pg = new Pagination();//默认分页组件
            var list = db.sysChangeLog
                .OrderByDescending(m => m.GreateTime)
                .Skip((pg.PageNum - 1) * pg.NumPerPage)
                .Take(pg.NumPerPage).ToList();

            foreach (var item in list)
            {
                item.ChangeContent = item.ChangeContent.Length > 30
                                         ? item.ChangeContent.Substring(0, 30)
                                         : item.ChangeContent;
            }
            pg.TotalCount = db.sysChangeLog.Count();
            ViewBag.Pagination = pg;
            return View(list);
        }
        public ViewResult AllDetails()
        {
            return View(db.sysChangeLog.OrderByDescending(m=>m.GreateTime).ToList());
        }
        [HttpPost]
        public ViewResult Index(Pagination pg)
        {
            var list = db.sysChangeLog
                .OrderByDescending(m => m.GreateTime)
                .Skip((pg.PageNum - 1) * pg.NumPerPage)
                .Take(pg.NumPerPage).ToList();
            foreach (var item in list)
            {
                item.ChangeContent = item.ChangeContent.Length > 30
                                         ? item.ChangeContent.Substring(0, 30)
                                         : item.ChangeContent;
            }
            pg.TotalCount = db.sysChangeLog.Count();
            ViewBag.Pagination = pg;
            return View(list);
        }
        //
        // GET: /ChangeLog/Details/5

        public ViewResult Details(Guid id)
        {
            var syschangelog = db.sysChangeLog.Single(s => s.ChangeLogID == id);
            return View(syschangelog);
        }

        //
        // GET: /ChangeLog/Create

        public ActionResult Create()
        {
            var model = new sysChangeLog { GreateTime = DateTime.Now };
            return View(model);
        } 

        //
        // POST: /ChangeLog/Create
        [HttpPost]
        public ActionResult Create(sysChangeLog syschangelog)
        {
            if (ModelState.IsValid)
            {
                syschangelog.ChangeLogID = Guid.NewGuid();
                db.sysChangeLog.AddObject(syschangelog);
                db.SaveChanges();
                return JavaScript(@"alertMsg.correct('保存成功');navTab.closeTab(""ChangeLogCreate"");navTab.reload(""ChangeLog"");");
            }
            return View(syschangelog);
        }
        
        //
        // GET: /ChangeLog/Edit/5
        public ActionResult Edit(Guid id)
        {
            sysChangeLog syschangelog = db.sysChangeLog.Single(s => s.ChangeLogID == id);
            return View(syschangelog);
        }

        //
        // POST: /ChangeLog/Edit/5
        [HttpPost]
        public ActionResult Edit(sysChangeLog syschangelog)
        {
            if (ModelState.IsValid)
            {
                db.sysChangeLog.Attach(syschangelog);
                db.ObjectStateManager.ChangeObjectState(syschangelog, EntityState.Modified);
                db.SaveChanges();
                //return RedirectToAction("Index");
                return JavaScript(@"alertMsg.correct('保存成功');navTab.closeTab(""ChangeLogEdit"");navTab.reload(""ChangeLog"");");
            }
            return View(syschangelog);
        }

        //
        // GET: /ChangeLog/Delete/5 
        public ActionResult Delete(Guid id)
        {
            sysChangeLog syschangelog = db.sysChangeLog.Single(s => s.ChangeLogID == id);
            return View(syschangelog);
        }

        //
        // POST: /ChangeLog/Delete/5
        [HttpPost, ActionName("Delete")]
        public JsonResult DeleteConfirmed(Guid id)
        {
            try
            {
                sysChangeLog syschangelog = db.sysChangeLog.Single(s => s.ChangeLogID == id);
                db.sysChangeLog.DeleteObject(syschangelog);
                if (db.SaveChanges() == 0) return Json(JosnMsg.ResultFail("删除失败！"));
            }
            catch(Exception ex)
            {
                return Json(JosnMsg.ResultFail("删除失败！"+ex));
            }
            return Json(JosnMsg.ResultOK("删除成功！"));
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}